package com.pignest.hotel.ui.room;

import java.util.ArrayList;
import java.util.List;

import com.pignest.hotel.beans.Room;
import com.pignest.hotel.definition.RoomRepairStatus;
import com.pignest.hotel.definition.RoomUseStatus;
import com.pignest.hotel.service.RoomService;
import com.pignest.hotel.spring.Services;
import com.pignest.hotel.system.L;
import com.pignest.hotel.system.Logic;

/**
 * 用于清洁人员改变房间使用状态(赃, 干净)
 * */
public class RoomStatusHistogram extends RoomHistogram {
	private static final long serialVersionUID = 6002348821960125969L;
	
	public RoomStatusHistogram() {
	}

	@Override
	protected List<Room> getRooms() {
		setShowFlags(RoomView.SHOW_ALL & ~(RoomView.SHOW_LIVING | RoomView.SHOW_BOOKING | RoomView.SHOW_REMARK));
		RoomService roomService = Services.getRoomService();
		return roomService.listAllRooms();
	}
	
	public void setUseStatus(RoomUseStatus status) {
		List<String> arr = getSelected();
		if (arr.size() == 0)
			return;
		ArrayList<Room> rooms = new ArrayList<Room>();
		ArrayList<RoomView> roomViews = new ArrayList<RoomView>();
		for (String roomId : arr) {
			RoomView view = getView(roomId);
			if (view == null)
				continue;
			Room room = view.getRoom();
			if (!Logic.canChangeStatus(room, status))
				continue;
			room.setRoomUseStatus(status);
			rooms.add(room);
			roomViews.add(view);
		}
		if (rooms.isEmpty())
			return;
		Services.getRoomService().saveAll(rooms);
		for (RoomView view : roomViews) {
			view.update();
		}
		L.update(rooms, "修改使用状态");
	}

	public void setRepairStatus(RoomRepairStatus status) {
		List<String> arr = getSelected();
		if (arr.size() == 0)
			return;
		ArrayList<Room> rooms = new ArrayList<Room>();
		ArrayList<RoomView> roomViews = new ArrayList<RoomView>();
		for (String roomId : arr) {
			RoomView view = getView(roomId);
			if (view == null)
				continue;
			Room room = view.getRoom();
			if (!Logic.canChangeStatus(room, status))
				continue;
			room.setRepairStatus(status.name());
			rooms.add(room);
			roomViews.add(view);
		}
		if (rooms.isEmpty())
			return;
		Services.getRoomService().saveAll(rooms);
		for (RoomView view : roomViews) {
			view.update();
		}
		L.update(rooms, "修改维修状态");
	}
}
